{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 光流估计\n",
    "\n",
    "光流是空间运动物体在观测成像平面上的像素运动的“瞬时速度”，根据各个像素点的速度矢量特征，可以对图像进行动态分析，例如目标跟踪。\n",
    "\n",
    "- 亮度恒定：同一点随着时间的变化，其亮度不会发生改变。\n",
    "\n",
    "- 小运动：随着时间的变化不会引起位置的剧烈变化，只有小运动情况下才能用前后帧之间单位位置变化引起的灰度变化去近似灰度对位置的偏导数。\n",
    "\n",
    "- 空间一致：一个场景上邻近的点投影到图像上也是邻近点，且邻近点速度一致。因为光流法基本方程约束只有一个，而要求x，y方向的速度，有两个未知变量。所以需要连立n多个方程求解。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](lk_2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](lk_1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lucas-Kanade 算法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](lk_3.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如何求解方程组呢？看起来一个像素点根本不够，在物体移动过程中还有哪些特性呢？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](lk_4.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### cv2.calcOpticalFlowPyrLK():\n",
    "参数：\n",
    "- prevImage 前一帧图像\n",
    "\n",
    "- nextImage 当前帧图像\n",
    "\n",
    "- prevPts 待跟踪的特征点向量\n",
    "\n",
    "- winSize 搜索窗口的大小\n",
    "\n",
    "- maxLevel 最大的金字塔层数\n",
    "\n",
    "返回：\n",
    "\n",
    "- nextPts 输出跟踪特征点向量\n",
    "\n",
    "- status 特征点是否找到，找到的状态为1，未找到的状态为0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "\n",
    "cap = cv2.VideoCapture('test.avi')\n",
    "\n",
    "# 角点检测所需参数\n",
    "feature_params = dict( maxCorners = 100,\n",
    "                       qualityLevel = 0.3,\n",
    "                       minDistance = 7)\n",
    "\n",
    "# lucas kanade参数\n",
    "lk_params = dict( winSize  = (15,15),\n",
    "                  maxLevel = 2)\n",
    "\n",
    "# 随机颜色条\n",
    "color = np.random.randint(0,255,(100,3))\n",
    "\n",
    "# 拿到第一帧图像\n",
    "ret, old_frame = cap.read()\n",
    "old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)\n",
    "# 返回所有检测特征点，需要输入图像，角点最大数量（效率），品质因子（特征值越大的越好，来筛选）\n",
    "# 距离相当于这区间有比这个角点强的，就不要这个弱的了\n",
    "p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)\n",
    "\n",
    "# 创建一个mask\n",
    "mask = np.zeros_like(old_frame)\n",
    "\n",
    "while(True):\n",
    "    ret,frame = cap.read()\n",
    "    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "    # 需要传入前一帧和当前图像以及前一帧检测到的角点\n",
    "    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)\n",
    "\n",
    "    # st=1表示\n",
    "    good_new = p1[st==1]\n",
    "    good_old = p0[st==1]\n",
    "    \n",
    "    # 绘制轨迹\n",
    "    for i,(new,old) in enumerate(zip(good_new,good_old)):\n",
    "        a,b = ((new).ravel()).astype(np.int)\n",
    "        c,d = ((old).ravel()).astype(np.int)\n",
    "        #print(a,b)\n",
    "        mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)\n",
    "        frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1)\n",
    "    img = cv2.add(frame,mask)\n",
    "\n",
    "    cv2.imshow('frame',img)\n",
    "    k = cv2.waitKey(150) & 0xff\n",
    "    if k == 27:\n",
    "        break\n",
    "\n",
    "    # 更新\n",
    "    old_gray = frame_gray.copy()\n",
    "    p0 = good_new.reshape(-1,1,2)\n",
    "\n",
    "cv2.destroyAllWindows()\n",
    "cap.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
